package com.ruoyi.weigh.service.impl;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.weigh.domain.Photo;
import com.ruoyi.weigh.hikvision.HCNetSDK;
import com.ruoyi.weigh.hikvision.HCNetSDK.*;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.weigh.mapper.SysWeighMapper;
import com.ruoyi.weigh.domain.SysWeigh;
import com.ruoyi.weigh.service.ISysWeighService;

/**
 * 称重记录Service业务层处理
 * 
 * @author yj
 * @date 2023-11-21
 */
@Service
public class SysWeighServiceImpl implements ISysWeighService 
{
    @Autowired
    private SysWeighMapper sysWeighMapper;

    /**
     * 查询称重记录
     * 
     * @param id 称重记录主键
     * @return 称重记录
     */
    @Override
    public SysWeigh selectSysWeighById(Long id)
    {
        return sysWeighMapper.selectSysWeighById(id);
    }

    /**
     * 查询称重记录列表
     * 
     * @param sysWeigh 称重记录
     * @return 称重记录
     */
    @Override
    public List<SysWeigh> selectSysWeighList(SysWeigh sysWeigh)
    {
        return sysWeighMapper.selectSysWeighList(sysWeigh);
    }

    @Override
    public List<SysWeigh> selectSysReceiptList(SysWeigh sysWeigh) {
        return sysWeighMapper.selectSysReceiptList(sysWeigh);
    }

    /**
     * 新增称重记录
     * 
     * @param sysWeigh 称重记录
     * @return 结果
     */
    @Override
    public int insertSysWeigh(SysWeigh sysWeigh)
    {
        sysWeigh.setCreateTime(DateUtils.getNowDate());
        return sysWeighMapper.insertSysWeigh(sysWeigh);
    }

    /**
     * 修改称重记录
     * 
     * @param sysWeigh 称重记录
     * @return 结果
     */
    @Override
    public int updateSysWeigh(SysWeigh sysWeigh)
    {
        sysWeigh.setUpdateTime(DateUtils.getNowDate());
        return sysWeighMapper.updateSysWeigh(sysWeigh);
    }

    /**
     * 批量删除称重记录
     * 
     * @param ids 需要删除的称重记录主键
     * @return 结果
     */
    @Override
    public int deleteSysWeighByIds(Long[] ids)
    {
        return sysWeighMapper.deleteSysWeighByIds(ids);
    }

    /**
     * 删除称重记录信息
     * 
     * @param id 称重记录主键
     * @return 结果
     */
    @Override
    public int deleteSysWeighById(Long id)
    {
        return sysWeighMapper.deleteSysWeighById(id);
    }

    //调用海康摄像头拍照返回路径
    @Override
    public String hikPhoto() {
        Photo photo = new Photo();
        File f=new File(photo.getFileRoot());
        if(!f.exists()){
            f.mkdirs();
        }
        HCNetSDK.NET_DVR_DEVICEINFO lpDeviceInfo=new HCNetSDK.NET_DVR_DEVICEINFO();
        //加载属性文件
        //loadProperties();
        //创建海康SDK实例
        HCNetSDK hk = HCNetSDK.INSTANCE	;
        //SDK初始化
        boolean init=hk.NET_DVR_Init();
        int userId=hk.NET_DVR_Login(photo.getSDVRIP(), photo.getSDVRPort(), photo.getSUserName(), photo.getSPassword(), lpDeviceInfo);
        NET_DVR_IPPARACFG ipparacfg=new NET_DVR_IPPARACFG();

        ipparacfg.write();
        Pointer lpIpParaConfig = ipparacfg.getPointer();
        IntByReference lpBytesReturned = new IntByReference(0);//获取IP接入配置参数
        boolean configIsOk=hk.NET_DVR_GetDVRConfig(userId, hk.NET_DVR_GET_IPPARACFG, 0, lpIpParaConfig, ipparacfg.size(), lpBytesReturned);
        ipparacfg.read();

        if (!configIsOk){//设备不支持,则表示没有IP通道
            for (int iChannum = 0; iChannum < lpDeviceInfo.byChanNum; iChannum++) {
                System.out.println(("Camera" + (iChannum +" not support "+ lpDeviceInfo.byStartChan)));
            }
        }else{//设备支持IP通道
            for (int iChannum = 0; iChannum < lpDeviceInfo.byChanNum; iChannum++){
                if(ipparacfg.byAnalogChanEnable[iChannum] == 1){
                    System.out.println(("Camera" + (iChannum +" 支持  "+ lpDeviceInfo.byStartChan)));
                }
            }
            for(int iChannum =0; iChannum < HCNetSDK.MAX_IP_CHANNEL; iChannum++){
                if (ipparacfg.struIPChanInfo[iChannum].byEnable == 1)
                {
                    System.out.println("IPCamera" + (iChannum + lpDeviceInfo.byStartChan)+" ip:"+new String(ipparacfg.struIPDevInfo[iChannum].struIP.sIpV4).trim()+
                            " byChannel="+ipparacfg.struIPChanInfo[iChannum].byChannel+" byEnable="+ipparacfg.struIPChanInfo[iChannum].byEnable+
                            " byIPID="+ipparacfg.struIPChanInfo[iChannum].byIPID);
                }
            }
        }
        System.out.println("登录的userId="+userId);
        NativeLong tdh=new NativeLong(1);
        NET_DVR_JPEGPARA cs=new NET_DVR_JPEGPARA();
        cs.wPicSize= photo.getWPicSize();
        cs.wPicQuality= photo.getWPicQuality();
        // 获取当前时间
        Date currentDate = new Date();
        // 创建SimpleDateFormat对象，指定日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        // 将日期对象格式化为字符串
        String currentTimeString = dateFormat.format(currentDate);
        String fileName=photo.getFileRoot()+"/"+currentTimeString+".jpeg";
        try {
            byte[] fileByte = fileName.getBytes("UTF-8");
            boolean pz=hk.NET_DVR_CaptureJPEGPicture(userId, tdh.intValue(), cs, fileByte);
            System.out.println("拍照通道号"+tdh+"："+(pz?"拍照成功照片在"+fileName:"拍照失败！！失败代码："+hk.NET_DVR_GetLastError()));
            if (pz == false){
                System.out.println("抓图失败,错误码为:" + hk.NET_DVR_GetLastError());
                return null;
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        hk.NET_DVR_Logout(userId);
        hk.NET_DVR_Cleanup();
        return fileName;
    }
}
